﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xpus.YouYTube.Data;

namespace Xpus.YouYTube.WebSite.Controllers
{
	public class VoteController : Controller
	{
		enum VideoVoteType { Agree, Disagree }
		enum CommentVoteType { Agree, Disagree, Report }

		/// <summary>
		/// 顶一个视频。
		/// </summary>
		/// <param name="id"></param>
		public void AgreeVideo(int id)
		{
			VoteVideo(id, VideoVoteType.Agree);
		}

		/// <summary>
		/// 踩一个视频。
		/// </summary>
		/// <param name="id"></param>
		public void DisagreeVideo(int id)
		{
			VoteVideo(id, VideoVoteType.Disagree);
		}

		/// <summary>
		/// 定一个回复。
		/// </summary>
		/// <param name="id"></param>
		public void AgreeComment(int id)
		{
			VoteComment(id, CommentVoteType.Agree);
		}

		/// <summary>
		/// 踩一个回复。
		/// </summary>
		/// <param name="id"></param>
		public void DisagreeComment(int id)
		{
			VoteComment(id, CommentVoteType.Disagree);
		}

		/// <summary>
		/// 举报一个回复。
		/// </summary>
		/// <param name="id"></param>
		public void ReportComment(int id)
		{
			VoteComment(id, CommentVoteType.Report);
		}

		private void VoteVideo(int id, VideoVoteType type)
		{
			string ip = Request.UserHostAddress;

			using (TDataContext db = new TDataContext())
			{
				// 30秒内不许再次操作
				var ipo = db.TIpVideoOperations.FirstOrDefault(ivo => ivo.IpAddress == ip && ivo.VideoId == id);

				if (ipo != null && ipo.LastVisitTime > DateTime.Now.AddSeconds(-30))
				{
					ipo.LastVisitTime = DateTime.Now;
					db.SubmitChanges();

					Response.Write("F30秒内请不要重复操作。");
					Response.End();
					return;
				}

				// 找到待操作的视频
				var video = db.TVideos.FirstOrDefault(v => v.Id == id);

				if (video == null)
				{
					Response.Write("F目标视频不存在。");
					Response.End();
					return;
				}

				// 记录操作的次数
				if (type == VideoVoteType.Agree)
					video.AgreeNumber++;
				else if (type == VideoVoteType.Disagree)
					video.DisagreeNumber++;

				// 记录操作时间
				if (ipo != null)
					ipo.LastVisitTime = DateTime.Now;
				else
				{
					ipo = new TIpVideoOperation()
					{
						IpAddress = ip,
						VideoId = id,
						LastVisitTime = DateTime.Now,
					};
					db.TIpVideoOperations.InsertOnSubmit(ipo);
				}

				db.SubmitChanges();

				Response.Write(string.Format("T{0},{1}", video.AgreeNumber, video.DisagreeNumber));
				Response.End();
			}
		}

		private void VoteComment(int id, CommentVoteType type)
		{
			string ip = Request.UserHostAddress;

			using (TDataContext db = new TDataContext())
			{
				// 30秒内不许再次操作
				var ipo = db.TIpCommentOperations.FirstOrDefault(ico => ico.IpAddress == ip && ico.CommentId == id);

				if(ipo != null && ipo.LastVisitTime > DateTime.Now.AddSeconds(-30))
				{
					ipo.LastVisitTime = DateTime.Now;
					db.SubmitChanges();

					Response.Write("F30秒内请不要重复操作。");
					Response.End();
					return;
				}

				// 找到待操作的回复
				var comment = db.TComments.FirstOrDefault(c => c.Id == id);

				if (comment == null)
				{
					Response.Write("F目标回复不存在。");
					Response.End();
					return;
				}

				// 记录操作次数
				if (type == CommentVoteType.Agree)
					comment.AgreeNumber++;
				else if (type == CommentVoteType.Disagree)
					comment.DisagreeNumber++;
				else if (type == CommentVoteType.Report)
					comment.ReportNumber++;

				// 记录操作时间
				if (ipo != null)
					ipo.LastVisitTime = DateTime.Now;
				else
				{
					ipo = new TIpCommentOperation()
					{
						IpAddress = ip,
						CommentId = id,
						LastVisitTime = DateTime.Now,
					};
					db.TIpCommentOperations.InsertOnSubmit(ipo);
				}

				db.SubmitChanges();

				Response.Write(string.Format("T{0},{1}", comment.AgreeNumber, comment.DisagreeNumber));
			}
		}
	}
}
